package controller

import (
	"fmt"
	"net/http"
	"strconv"
	"strings"

	"github.com/kataras/iris/context"

	"im/dao"
)

type Contents struct {
	Base
}

// swagger:route GET /contents/{id}.html posts get-posts-content
//   获取帖子详情h5页面
//
//   获取帖子详情h5页面
//   Security:
//   Responses:
//	    200:
func (c Contents) Get() {
	idStr := c.Ctx.Params().GetString("id")
	idStr = strings.Split(idStr, ".")[0]
	id, _ := strconv.ParseInt(idStr,10,64)
	post, err := dao.NewPostsWithEs().GetById(id)
	if err != nil {
		c.Ctx.StatusCode(http.StatusBadRequest)
		return
	}
	htmlTpl := `<html>
		<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=no"/>
		<head>
		</head>
		<body class="container" style="padding: 20px;">
			<div class="title">
				<h1 style="font-size: 20px;font-weight: 400;word-break: break-word;">%s</h1>
			</div>
			<div class="content">
				<p style="margin-bottom: 16px;word-break: break-word;font-weight: 400;line-height: 1.8;">%s</p>
			</div>
		</body>
	</html>
`
	html := fmt.Sprintf(htmlTpl, post.Title, post.Content)
	c.Ctx.ContentType(context.ContentHTMLHeaderValue)
	_, _ = c.Ctx.WriteString(html)
}
